/**
  editormd :

  github：
  https://github.com/pandao/editor.md
  
  官网：
  https://pandao.github.io/editor.md/

  完整示例：
  https://pandao.github.io/editor.md/examples/full.html
 */
$(function () {

  ajax.get("/blog/getAccessCount?blogid=" + blogid, {
    // data: $(data.form).serialize(),
    success: function (resp) {
      if (resp.ok) {
        $("#accessCount").html(resp.data);
      } else {
        //layer.alert(resp.msg || "保存失败");
      }
    }
  });

  var testEditor = editormd.markdownToHTML("html-content-blog-view", {
    //注意：这里是上面DIV的id
    htmlDecode: "style,script,iframe",
    emoji: true,
    taskList: true,
    tex: true, // 默认不解析
    flowChart: true, // 默认不解析
    sequenceDiagram: true, // 默认不解析
    codeFold: true
  });

  // html-content-blog-view
  //先循环一遍构造layer.photos需要的json数据
  var lay_photos_data = [];
  $("#html-content-blog-view img").each(function (i, _img) {
    var img = $(_img);
    if (img.parent().is("a")) {
      return;
    }

    var img_id = "lay_photos_blog_img_" + blogid + "_" + i;
    var smurl = img.attr("src");
    var url = img.attr("src");
    lay_photos_data[i] = {
      alt: img.attr("alt") || img.attr("title") || "I love tio",
      pid: img_id,
      src: res_url(url), //原图地址
      thumb: res_url(smurl) //缩略图地址
    };
    img.css("cursor", "pointer");
    img.attr("id", img_id);
  });

  var _imgclick = function (photos_options) {
    this.h = function () {
      layer.photos(photos_options);
    };
  };

  $("#html-content-blog-view img").each(function (i, _img) {
    var img = $(_img);
    if (img.parent().is("a")) {
      return;
    }
    // var img_id = "lay_photos_blog_img_" + blogid + "_" + i;
    //点击图片时的处理者
    var hh = new _imgclick({
      photos: {
        title: "", //相册标题
        id: blogid, //相册id
        start: i, //初始显示的图片序号，默认0
        data: lay_photos_data
      }, //格式见API文档手册页
      anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
    }).h;
    //给前面的a链接添加click事件，动态添加的dom，所以用下面这种方法添加事件
    // img.on("click", "#" + img_id, hh);
    img.on("click", hh);
  });


  /**
   * 给所有链接在新窗口中打开
   */
  $("#html-content-blog-view a").each(function (i, a) {
    $(a).attr("target", "_blank");
  });

  if (curruser && blogauthor == curruser.id) {
    $("#modify_blog_wrap").css("display", ""); //博客修改按钮
  }
});










$(function () {
  waterMark$();
});
function waterMark$() {
  if (!curruser) {
    return;
  }
  if (waterAdded) {
    return;
  }
  waterAdded = true;

  var waterContent = curruser.id + formatDateByTime(new Date().getTime(), "yyMMddhhmm");

  if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/11./i) != "11.") {
    $("p[name='p1$']").remove();
    var winwidth$ = document.body.scrollWidth - 17;
    var winheight$ = document.body.scrollHeight;
    // $("body").append("<p id='waterSum_11' name='p1$' class='cover_through cover js-click-to-alert'>测试张璐</p>");
    var fleft = Number($('#waterSum_11').css("margin-left").substring(0, $('#waterSum_11').css("margin-left").indexOf('p')));
    var ftop = Number($('#waterSum_11').css("margin-top").substring(0, $('#waterSum_11').css("margin-top").indexOf('p')));
    var perWidth = $("#waterSum_11").width();
    var perHeight = Number('140px'.substring(0, '140px'.indexOf('p'))) + 100;
    var lines = parseInt(winwidth$ / (perWidth + fleft));
    var rows = Math.round(winheight$ / (perHeight + ftop));
    var totalPWidth = perWidth * lines;
    var totalSpace = winwidth$ - totalPWidth;
    var perSpace = parseInt(totalSpace / (lines + 1));
    $('#waterSum_11').css("margin-left", perSpace);
    for (var i = 1; i <= rows; i++) {
      for (var j = 1; j <= lines; j++) {
        var p = "<p id='waterSum_" + i + "" + (j + 1) + "' name='p1$' class='cover_through cover js-click-to-alert'>" + waterContent + "</p>";
        var ileft = $('#waterSum_' + i + '' + j).css("margin-left").substring(0, $('#waterSum_' + i + '' + j).css("margin-left").indexOf('p'));
        var itop = $('#waterSum_11').css("margin-top").substring(0, $('#waterSum_11').css("margin-top").indexOf('p'));
        $("body").append(p);
        $('#waterSum_' + i + '' + (j + 1)).css("margin-left", Number(ileft) + Number(perWidth) + perSpace);
        $('#waterSum_' + i + '' + (j + 1)).css('margin-top', itop);
      }
    }
    passThrough();
  } else {
    waterMarkNotIe$();
  }
}
function waterMarkNotIe$() {
  var winwidth$ = document.body.clientWidth;
  var winheight$ = document.body.scrollHeight;
  var waterSum$ = 100;
  var oldleft$ = 0;
  var maxI$ = 0;
  var k$ = 0;
  var waterContent = curruser.id + formatDateByTime(new Date().getTime(), "yyMMddhhmm");

  $("body").append("<div class='cover-water-area'> </div>");
  $('.cover-water-area').css('height', '1500px');
  for (var i = 1; i <= waterSum$; i++) {
    $(".cover-water-area").append("<p id='waterSum_" + i + "' class='cover_through cover-water-link js-click-to-alert'>" + waterContent + "</p>");
    var left = Number(document.getElementById("waterSum_" + i).offsetLeft);
    if (left > oldleft$) {
      oldleft$ = left;
      maxI$ = i;
    }
    if (left < oldleft$ && k$ == 0) {
      var top = $("#waterSum_1").css("margin-top").substring(0, $("#waterSum_1").css("margin-top").indexOf('p'));
      var bottom = $("#waterSum_1").css("margin-bottom").substring(0, $("#waterSum_1").css("margin-bottom").indexOf('p'));
      var pHeight = $("#waterSum_1").height();
      var totalHeight = Number(top) + Number(pHeight) + Number(bottom);
      var Hnum = Math.round(500 / (totalHeight / 1.3));
      waterSum$ = Hnum * maxI$;
      k$++;
    }
  }
}
window.onresize = function () {
  waterMark$();
}
function passThrough() {
  $(".cover").mouseenter(function () {
    $(this).stop(true).fadeOut().delay(1500).fadeIn(50);
  });
}